<?php
session_start();
require_once("../models/Admin.php");
require_once("../models/Unit.php");
class ChangePasswordController{

	var $passwordDetails;
	function sendChangePasswordData(){
		$this->passwordDetails['oldpass'] = $_REQUEST['oldpass'];
		$this->passwordDetails['newpass'] = $_REQUEST['newpass'];
		$this->passwordDetails['repeat'] = $_REQUEST['repeat'];
		$this->passwordDetails['unit_name'] = $_REQUEST['unit_name'];
	}
	function isMatched(){
		if($this->passwordDetails['newpass']==$this->passwordDetails['repeat']){
			return true;
		}
		else return false;
	}
	
	function isValidLoginInput(){
		if($this->passwordDetails['oldpass']==""||$this->passwordDetails['newpass']==""||$this->passwordDetails['repeat']==""){
			return false;
		}
		else return true;
	}
	
}

	$a = new changePasswordController;
	$a->sendChangePasswordData();
	$admin= new Admin;
	$admin->connect();
	if($_REQUEST['mode']==2){
		$unit = new Unit;
		$unit->connect();
		$unit->setPassword($a->passwordDetails);
		echo "Successfully changed password for Unit ".$a->passwordDetails['unit_name'].".";
		die;
	}
	if(!$a->isValidLoginInput()){
		echo "Invalid Input!";
		die;
	}
	if(!$a->isMatched()){
		echo "Passwords do not match!";
		die;
	}
	$loginDetails['username']="admin";
	$loginDetails['password']=$a->passwordDetails['oldpass'];
	if(!$admin->isValidAdminLogin($loginDetails)){
		echo "Incorrect Password!";
		die;
	}
	$admin->setPassword(base64_encode($a->passwordDetails['newpass']));
	echo "Password changed. Email is sent to your email address.";
	


	
?>